/*
 * SwingUtilities.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import java.lang.reflect.InvocationTargetException;

/**
 * Swing Utilities.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public final class SwingUtilities {

// Static Initialization. ------------------------------------------------------

  static {
    Swing.initialize();
  }

// Private Constructor. --------------------------------------------------------

 /**
  * Class has no instances.
  */
  private SwingUtilities() {
    // empty constructor body
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Invokes runnable target from the  GUI loop.
   *
   * @param target the runnable target
   *
   * @throws IllegalArgumentException if the target is <tt>null</tt>
   */
  public static void invokeLater(Runnable target) {
    if (target == null) {
      throw new IllegalArgumentException("Null target");
    }
    _invokeLater(target);
  }

  /**
   * Invokes runnable target from the GUI loop and waits until finish.
   * <p>
   *   <tt>NOTE: Do not call this method form Swing thread.</tt>
   * </p>
   *
   * @param target the runnable target
   *
   * @throws IllegalArgumentException if the target is <tt>null</tt>
   * @throws Error if the method is called from the Swing thread.
   * @throws InvocationTargetException if an exception is thrown while running
   *                                   <tt>target</tt>
   */
  public static void invokeAndWait(Runnable target) throws
                                                     InvocationTargetException {
    if (target == null) {
      throw new IllegalArgumentException("Null target");
    }
    if (Swing.isSwingThread()) {
      throw new Error("Method invokeSafeAndWait() called " + // NOPMD
                      "from the Swing thread");
    }
    _invokeAndWait(target);
  }

  /**
   * Invokes "safe" runnable target from the GUI loop and waits until finish.
   * Exceptions are ignored and printed on the standard error stream. "Safe"
   * runnable target doesn't throw exceptions.
   * <p>
   *   <tt>NOTE: Do not call this method form the Swing thread.</tt>
   * </p>
   *
   * @param safeTarget the runnable target
   *
   * @throws IllegalArgumentException if the target is <tt>null</tt>
   * @throws Error if the method is called from the Swing thread.
   */
  public static void invokeSafeAndWait(Runnable safeTarget) {
    if (safeTarget == null) {
      throw new IllegalArgumentException("Null target");
    }
    if (Swing.isSwingThread()) {
      throw new Error("Method invokeSafeAndWait() called " + // NOPMD
                      "from the Swing thread");
    }
    _invokeSafeAndWait(safeTarget);
  }

  /**
   * Returns the index of mnemonic '&'.
   *
   * @param text the text
   *
   * @return mnemonic index
   */
  public static int getMnemonicIndex(String text) {
    if (text != null) {
      int lastChar = text.length();
      --lastChar;
      for (int i = 0; i < lastChar;) {
        if (text.charAt(i) == '&') {
          ++i;
          if (text.charAt(i) != '&') { // NOPMD
            return --i;
          }
        }
        ++i;
      }
    }
    return -1;
  }

// Private Static Native Methods. ----------------------------------------------

  private static native void _invokeLater(Runnable target);

  private static native void _invokeAndWait(Runnable target) throws
                                                      InvocationTargetException;

  private static native void _invokeSafeAndWait(Runnable safeTarget);

}
